import {ActionContext, Module} from 'vuex';
import Node from '@/store/entities/flow/flow-data/node';
import Line from '@/store/entities/flow/flow-data/line';
import Info from '@/store/entities/flow/flow-data/info';

interface FlowDataState {
    name: string;
    nodeList: Node[];
    lineList: Line[];
}

class FlowDataModule implements Module<FlowDataState, any> {
    namespaced = true;
    state = {
        name: '',
        nodeList: [],
        lineList: [],
    };
    mutations = {
        changeName(state:FlowDataState,payload:any){
            state.name=payload.name;
        },
        addNode(state: FlowDataState, payload: any) {
            state.nodeList.push(payload.node);
        },
        addLine(state: FlowDataState, payload: any) {
            state.lineList.push(payload.line);
        },
        deleteNode(state: FlowDataState, payload: any) {
            state.nodeList = state.nodeList.filter((item) => {
                return item.id !== payload.nodeId;
            });
        },
        deleteLine(state: FlowDataState, payload: any) {
            state.lineList = state.lineList.filter((item) => {
                return !(item.from === payload.from && item.to === payload.to);
            });
        },
        reset(state: FlowDataState, payload: any) {
            state.name='';
            state.nodeList = [];
            state.lineList = [];
        },
        init(state: FlowDataState, payload: any) {
            state.name = payload.name;
            state.nodeList = payload.nodeList;
            state.lineList = payload.lineList;

        },
    };
}

const flowDataModule = new FlowDataModule();
export default flowDataModule;
